var xl = require('node-xlrd');
var XMLWriter = require('xml-writer');
var fs = require('fs');
var moment = require('moment');
var md5 = require('md5');

function createXml(fn) {
  var dateNow = moment().format('YYYYMMDDHHmmss');
  console.log(fn+'--------excel');
  var seriaNo = (moment().millisecond() * 100).toString().substr(0,2);
  var ws = fs.createWriteStream('./public/xml/'+ dateNow + seriaNo + '.xml');
  var xw = new XMLWriter(false, function (string,encoding) {
    ws.write(string, encoding);
  });
  
  xl.open('./' + fn,function(err,bk){
    if(err){ console.log(err.name, err.message); return;}
    var sht1 = bk.sheets[0];
    var sht2 = bk.sheets[1];
    var sbdNo = '4109002' + sht2.cell(5, 7) + dateNow + seriaNo;
    xw.startDocument('1.0', 'utf-8').startElement('ENT801Message')
      .writeAttribute('xmlns', 'http://www.chinaport.gov.cn/ENT')
      .writeAttribute('sendCode', 'sendcode')
      .writeAttribute('reciptCode', 'reciptcode');
    xw.startElement('Application');
    xw.startElement('ApplicationHead')
      .writeElement('ID',md5(Math.random()).toUpperCase())
      .writeElement('SBD_NO', sbdNo )
      .writeElement('SBQYBA_NO', sht2.cell.getValue(5, 7))
      .writeElement('WB_CODE', sht2.cell.getValue(6, 7) )
      .writeElement('IE_FLAG', 'I')
      .writeElement('MODIFY_MARK', 1)
      .writeElement('DSQY_CODE', sht2.cell.getValue(9, 7))
      .writeElement('DSQY_NAME', sht2.cell.getValue(10, 7))
      .writeElement('WLQY_CODE', sht2.cell.getValue(11, 7))
      .writeElement('WLQY_NAME', sht2.cell.getValue(12, 7))
      .writeElement('SB_DATE', moment().format('YYYY-MM-DD HH:mm:ss'))
      .writeElement('LXR', sht2.cell.getValue(14, 7))
      .writeElement('LXR_PHONE', sht2.cell.getValue(15, 7))
      .writeElement('REC_NAME_CN', sht2.cell.getValue(16, 7))
      .writeElement('REC_NAME_EN', sht2.cell.getValue(17, 7))
      .writeElement('SEND_NAME_CN', sht2.cell.getValue(18, 7))
      .writeElement('SEND_NAME_EN', sht2.cell.getValue(19, 7))
      .writeElement('LOCAL_JYJYJG', sht2.cell.getValue(20, 7))
      .writeElement('QYKA_CODE', sht2.cell.getValue(21, 7))
      .writeElement('QYKA', sht2.cell.getValue(22, 7))
      .writeElement('RJKA_CODE', sht2.cell.getValue(23, 7))
      .writeElement('RJKA', sht2.cell.getValue(24, 7))
      .writeElement('TRANS_CODE', sht2.cell.getValue(25, 7))
      .writeElement('TRANS_NAME', sht2.cell.getValue(26, 7))
      .writeElement('TRANS_NO', sht2.cell.getValue(27, 7))
      .writeElement('TRADE_COUNTRY_CODE', sht2.cell.getValue(28, 7))
      .writeElement('TRADE_COUNTRY', sht2.cell.getValue(29, 7))
      .writeElement('QY_COUNTRY_CODE', sht2.cell.getValue(30, 7))
      .writeElement('QY_COUNTRY', sht2.cell.getValue(31, 7))
      .writeElement('ARR_DATE', moment(sht2.cell(33, 7),'YYYY/MM/DD').format('YYYY-MM-DD'))
      .writeElement('PACK_TYPE', sht2.cell.getValue(37, 7))
      .writeElement('PACK_TYPE_NAME', sht2.cell.getValue(38, 7))
      .writeElement('GROSS_WEIGHT', sht2.cell.getValue(40, 7))
      .writeElement('WEIGHT_TYPE', sht2.cell.getValue(41, 7))
      .writeElement('GOODS_NUM', sht2.cell.getValue(43, 7))
      .writeElement('NUM_TYPE', sht2.cell.getValue(44, 7))
      .writeElement('FJ_FLAG', sht2.cell.getValue(45, 7) == "否" ? 'N' : 'Y')
      .writeElement('ZWX_FLAG', sht2.cell.getValue(46, 7) == "否" ? 'N' : 'Y');
    xw.endElement();
    for(var line = 1; line< sht1.row.count; line++){
      xw.startElement('ApplicationList')
        .writeElement('ID',sht1.cell(line,0))
        .writeElement('ITEM_NO',sht1.cell(line,1))
        .writeElement('GOODS_NO',sht1.cell(line,2))
        .writeElement('SHEL_GOODS_NAME',sht1.cell(line,3))
        .writeElement('HS_CODE',sht1.cell(line,4))
        .writeElement('SB_GOODS_NAME',sht1.cell(line,5))
        .writeElement('GGXH',sht1.cell(line,6))
        .writeElement('CJ_PRICE',sht1.cell(line,7))
        .writeElement('CURRENCY_CODE',sht1.cell(line,8))
        .writeElement('CURRENCY',sht1.cell(line,9))
        .writeElement('QUANTITY',sht1.cell(line,10))
        .writeElement('PRICETOTAL',sht1.cell(line,11))
        .writeElement('UNIT_CODE',sht1.cell(line,12))
        .writeElement('UNIT',sht1.cell(line,13))
        .writeElement('GROSS_WEIGHT',sht1.cell(line,14))
        .writeElement('WEIGHT_UTIL',sht1.cell(line,15))
        .writeElement('FJ_FLAG',sht1.cell(line,16))
        .writeElement('ZWX_FLAG','N')
        .writeElement('ORIGIN_COUNTRY_CODE',sht1.cell(line,17))
        .writeElement('ORIGIN_COUNTRY',sht1.cell(line,18))
        .writeElement('PACK_TYPE',sht1.cell(line,19))
        .writeElement('PACK_NUM',sht1.cell(line,20))
        .writeElement('BD_VOYAGE_NO',sht2.cell(6,7));
      xw.endElement();
    }
    xw.endElement();
    xw.endElement();
    xw.endDocument();
    ws.end();
  });
  var ss;
  return ss = ws;
  console.log(ss)
}
exports.createXml = createXml;

